home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlfind.zip / BIGRAM.C < prev    next >
C/C++ Source or Header  |  1990-05-17  |  1KB  |  60 lines

  1. /* bigram -- list bigrams for fast-find
  2.  
  3.    Usage: bigram < text > bigrams
  4.    
  5.    Use 'code' to encode a file using this output.
  6.  
  7.    Author: James A. Woods (jaw@riacs.edu)
  8.    Modified by David MacKenzie (djm@ai.mit.edu)
  9.    Public domain. */
  10.  
  11. #include <stdio.h>
  12. #include <sys/types.h>
  13. #include <sys/param.h>
  14.  
  15. #ifndef MAXPATHLEN
  16. #define MAXPATHLEN 1024
  17. #endif
  18.  
  19. char path[MAXPATHLEN];
  20.  
  21. char oldpath[MAXPATHLEN] = " ";
  22.  
  23. void
  24. main ()
  25. {
  26.   register int count, j;
  27.  
  28.   while (fgets (path, sizeof path, stdin) != NULL)
  29.     {
  30.       path[strlen (path) - 1] = '\0'; /* Remove newline. */
  31.  
  32.       count = prefix_length (oldpath, path);
  33.       /* Output post-residue bigrams only. */
  34.       for (j = count; path[j] != '\0'; j += 2)
  35.     {
  36.       if (path[j + 1] == '\0')
  37.         break;
  38.       putchar (path[j]);
  39.       putchar (path[j + 1]);
  40.       putchar ('\n');
  41.     }
  42.       strcpy (oldpath, path);
  43.     }
  44.   exit (0);
  45. }
  46.  
  47. /* Return length of longest common prefix of strings S1 and S2. */
  48.  
  49. int
  50. prefix_length (s1, s2)
  51.      char *s1, *s2;
  52. {
  53.   register char *start;
  54.  
  55.   for (start = s1; *s1 == *s2; s1++, s2++)
  56.     if (*s1 == '\0')
  57.       break;
  58.   return s1 - start;
  59. }
  60.